/**
 * 
 */
package com.zhongkai.web.control.ajax;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.json.JSONObject;
import com.zhongkai.model.book.TCsCclxdz;
import com.zhongkai.model.book.TCsCcsjs;
import com.zhongkai.model.book.TCsCcsjsndqj;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDjNsrxx;
import com.zhongkai.service.book.CommonBookService;

/**
 * @packagename com.zhongkai.web.control.ajax
 * @filename CommonAjaxControl.java
 * @作者 吴嘉俊,胡宇峰
 * @创建日期 Aug 4, 2011 2:31:56 PM
 *
 */

@Controller
public class CommonAjaxControl {
	
	private CommonBookService commonBookService ;
	
	public CommonBookService getCommonBookService() {
		return commonBookService;
	}
	@Resource
	public void setCommonBookService(CommonBookService commonBookService) {
		this.commonBookService = commonBookService;
	}
	
	/*
	 * 判断车船牌照号是否重复
	 */

	@RequestMapping(value="/system/ajax/common_findCarNo.do")
	public void findCarNo(HttpServletResponse response,String ccpzh,String jjcclxDm,String ccdjh)throws Exception{
		PrintWriter out=response.getWriter();
			if(ccpzh==null||"".equals(ccpzh)){
				System.out.println(ccpzh+"车牌号码不能为空");
				out.write("<font color=\"red\">车牌号码不能为空!</font>");
				return;
			}
			
			ccpzh = "粤T." + ccpzh.toUpperCase();
			List list = commonBookService.FindCarNo(ccpzh,jjcclxDm,ccdjh);
			if(list==null||list.size()==0||list.isEmpty()){
				System.out.println(ccpzh+"该车牌号码有效");
				out.write("<font color=\"green\">该车牌号码有效!</font>");
			}
			else{
				System.out.println(ccpzh+"该车牌号码已登记");
				out.write("<font color=\"red\">该车牌号码已登记!</font>");
			}
	}
	/*
	 * 查找单位车船信息
	 */
	@RequestMapping(value="/system/ajax/common_findCarOwner.do")
	public String dwccck(HttpServletRequest request,HttpServletResponse response,String nsrbm) throws Exception
	{
		List<TDjCcdjxx> nsrxx=commonBookService.FindCarOwner(nsrbm);
		if (nsrxx==null || nsrxx.size()==0 ||nsrxx.isEmpty())
		{
			response.getWriter().write("NO");
		}
		else
		{
			JSONObject js=new JSONObject();
			for(Iterator insrxx=nsrxx.iterator();insrxx.hasNext();)
			{
				TDjNsrxx nsxx=(TDjNsrxx) insrxx.next();
				js.put("nsname", nsxx.getNsrmc());
				js.put("nsaddress", nsxx.getNsrdz());
				js.put("nsphone", nsxx.getNsrdh());
			}
			System.out.println(js.toString());
			response.getWriter().write(js.toString());
		
			
			
		}
		return null;
	}
	/*
	 * 车船自身信息
	 */
	@RequestMapping(value="/system/ajax/common_findCarInfo.do")
	public String ccxzxx(HttpServletRequest request,HttpServletResponse response,String jjcclxDm) throws Exception
	{
		System.out.println("aaaa");
		String swdm="";
		DateFormat df = new SimpleDateFormat("yyyy");
		Date date = new Date();
		String now = df.format(date);
		String period="";
		List<TCsCcsjsndqj> ccxzxxlist=commonBookService.FindTimePeriod();
		for(Iterator inccxz=ccxzxxlist.iterator();inccxz.hasNext();)
		{
			TCsCcsjsndqj ccxzsj=(TCsCcsjsndqj) inccxz.next();
			if (now.compareTo(ccxzsj.getNdxx())>0 && now.compareTo(ccxzsj.getNdsx())<=0)
			{
				period=ccxzsj.getJsnd();
				break;
			}
			
		}
		/*
		 * 搜索出税务代码
		 * 
		 * 
		 */
		List list=commonBookService.FindCwcclxDm(jjcclxDm);
		for(Iterator ilist=list.iterator();ilist.hasNext();)
		{
			TCsCclxdz sw=(TCsCclxdz) ilist.next();
			swdm=(String)sw.getSwcclxDm();
		}
		
		/*
		 * 搜索出整备质量等信息。
		 */
		List<TCsCcsjs> ccsjslist=commonBookService.FindInfo(swdm,period);
		double maxzb=-1;
		double minzb=-1;
		double maxzk=-1;
		double minzk=-1;
		double maxzz=-1;
		double minzz=-1;
		for(Iterator inccsjs=ccsjslist.iterator();inccsjs.hasNext();)
		{
			TCsCcsjs ccsjs=(TCsCcsjs) inccsjs.next();
			if (ccsjs.getZbzlSx()!=null) maxzb=ccsjs.getZbzlSx();
			if (ccsjs.getZbzlXx()!=null)minzb=ccsjs.getZbzlXx();
			if (ccsjs.getZklSx()!=null)maxzk=ccsjs.getZklSx();
			if (ccsjs.getZklXx()!=null)minzk=ccsjs.getZklXx();
			if (ccsjs.getZzlSx()!=null)maxzz=ccsjs.getZzlSx();
			if (ccsjs.getZzlXx()!=null)minzz=ccsjs.getZzlXx();
			
		}
		JSONObject js=new JSONObject();
		js.put("zbsx", maxzb);
		js.put("zbxx", minzb);
		js.put("zksx", maxzk);
		js.put("zkxx", minzk);
		js.put("zzsx", maxzz);
		js.put("zzxx", minzz);
		
		response.getWriter().write(js.toString());
		
			
		return null;
	}
	
}


